package com.waitingsnow.core.display.loader
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.util.display.BmpDataUtil;

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

/**
 * 
 * @author waitingsnow
 * $Id: BaseLoader.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class BaseLoader extends EventDispatcher
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>BaseLoader</code> instance.
	 * 
	 */
	public function BaseLoader()
	{
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var loader:Loader;
	private var url:String;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	/**
	 * load的附加信息
	 */	
	public var paramObject:Object;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	public function startLoad(url:String, domin:ApplicationDomain = null, checkPolice:Boolean = false):void
	{
		this.url = url;
		removeLoader();
		if (!loader)
		{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIoErrorHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		}
		loader.load(new URLRequest(url), new LoaderContext(checkPolice, domin));
	}
	
	public function getContent():DisplayObject
	{
		return loader.content;
	}
	
	public function getOriginalContent():DisplayObject
	{
		return loader.contentLoaderInfo.content;
	}
	
	public function getLoader():Loader
	{
		return loader;
	}
	
	public function getBmpDataArr(width:int = 0, height:int = 0):Array
	{
		return BmpDataUtil.getBmpDataArr(getContent(), width, height);
	}
	
	public function getBmpData(width:int = 0, height:int = 0):BitmapData
	{
		return BmpDataUtil.getBmpDataArr(getContent(), width, height)[0] as BitmapData;
	}
	
	public function startUrlLoader():void
	{
		
	}
	
	private function removeLoader():void
	{
		if (loader)
		{
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIoErrorHandler);
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
			loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			loader.unloadAndStop();
			loader = null;
		}
	}
	
	private function removeUrlLoader():void
	{
		
	}
	
	public function dispose():void
	{
		removeLoader();
		removeUrlLoader();
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function loaderCompleteHandler(event:Event):void
	{
		dispatchEvent(new Event(Event.COMPLETE));
	}
	
	private function loaderIoErrorHandler(event:IOErrorEvent):void
	{
		Logger.log("load url ioError: " + url);
	}
	
	private function securityErrorHandler(event:SecurityErrorEvent):void
	{
		Logger.log("load url securityError: " + url);
	}
}
}